package com.xunchi.haichuan.audit.service;

import java.util.Locale;

import com.xunchi.haichuan.audit.dao.ActionDAO;
import com.xunchi.haichuan.audit.dao.ActionDAOImpl;
import com.xunchi.haichuan.audit.dao.LogDAO;
import com.xunchi.haichuan.audit.dao.LogDAOImpl;
import com.xunchi.haichuan.audit.model.Action;
import com.xunchi.haichuan.audit.model.Log;
import com.xunchi.haichuan.user.model.User;
import com.xunchi.haichuan.utility.MiscUtil;

/**
 * 作者：kongxb<br>
 * 模块名称：审计模块<br>
 * 功能描述：审计日志服务实现<br>
 * 维护记录：2009.11.27, kongxb 创建该类
 */
public class LogServiceImpl implements LogService {

	private ActionDAO actionDAO;
	private LogDAO logDAO;

	/**
	 * 构造函数
	 */
	public LogServiceImpl() {
		actionDAO = new ActionDAOImpl(Action.class);
		logDAO = new LogDAOImpl(Log.class);
	}

	/*
	 * @see com.xunchi.haichuan.audit.service.LogService#login(com.xunchi.haichuan.user.model.User, int,
	 * java.lang.String, java.lang.String)
	 */
	public void login(User user, int operateStat, String descript, String ipAddress, Locale locale) throws Exception {
		Action action = actionDAO.getById("userLogin");
		if (action == null) {
			return;
		}
		action.setName(MiscUtil.replaceMsgKey(action.getName(), locale));
		action.setDescript(MiscUtil.replaceMsgKey(action.getDescript(), locale));
		log(action, user.getUsrId(), user.getName(), user.getLoginName(), 0, action.getName(), action.getName(),
				operateStat, descript, ipAddress);
	}

	/*
	 * @see com.xunchi.haichuan.audit.service.LogService#logout(com.xunchi.haichuan.user.model.User, int,
	 * java.lang.String, java.lang.String)
	 */
	public void logout(User user, int operateStat, String descript, String ipAddress, Locale locale) throws Exception {
		Action action = actionDAO.getById("userLogout");
		if (action == null) {
			return;
		}
		action.setName(MiscUtil.replaceMsgKey(action.getName(), locale));
		action.setDescript(MiscUtil.replaceMsgKey(action.getDescript(), locale));
		log(action, user.getUsrId(), user.getName(), user.getLoginName(), 0, action.getName(), action.getName(),
				operateStat, descript, ipAddress);
	}

	/*
	 * @see com.xunchi.haichuan.audit.service.LogService#log(java.lang.String, com.xunchi.haichuan.user.model.User, int)
	 */
	public void log(String actionId, User user, int operateStat) throws Exception {
		log(actionId, user.getUsrId(), user.getName(), user.getLoginName(), 0, null, null, operateStat, null, null);
	}

	/*
	 * @see com.xunchi.haichuan.audit.service.LogService#log(java.lang.String, com.xunchi.haichuan.user.model.User, int,
	 * java.lang.String)
	 */
	public void log(String actionId, User user, int operateStat, String descript) throws Exception {
		log(actionId, user.getUsrId(), user.getName(), user.getLoginName(), 0, null, null, operateStat, descript, null);
	}

	/*
	 * @see com.xunchi.haichuan.audit.service.LogService#log(java.lang.String, com.xunchi.haichuan.user.model.User,
	 * java.lang.String, java.lang.String, int)
	 */
	public void log(String actionId, User user, String operateObject, String operateObjectName, int operateStat)
			throws Exception {
		log(actionId, user, operateObject, operateObjectName, operateStat, null, null);
	}

	/*
	 * @see com.xunchi.haichuan.audit.service.LogService#log(java.lang.String, com.xunchi.haichuan.user.model.User,
	 * java.lang.String, java.lang.String, int, java.lang.String)
	 */
	public void log(String actionId, User user, String operateObject, String operateObjectName, int operateStat,
			String descript) throws Exception {
		log(actionId, user.getUsrId(), user.getName(), user.getLoginName(), 0, operateObject, operateObjectName,
				operateStat, descript, null);
	}

	/*
	 * @see com.xunchi.haichuan.audit.service.LogService#log(java.lang.String, com.xunchi.haichuan.user.model.User,
	 * java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)
	 */
	public void log(String actionId, User user, String operateObject, String operateObjectName, int operateStat,
			String descript, String ipAddress) throws Exception {
		log(actionId, user.getUsrId(), user.getName(), user.getLoginName(), 0, operateObject, operateObjectName,
				operateStat, descript, ipAddress);
	}

	/*
	 * @see com.xunchi.haichuan.audit.service.LogService#log(java.lang.String, java.lang.String, java.lang.String,
	 * java.lang.String, int, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)
	 */
	public void log(String actionId, String operatorId, String operatorName, String operatorLoginName,
			int operatorType, String operateObject, String operateObjectName, int operateStat, String descript,
			String ipAddress) throws Exception {
		Action action = actionDAO.getById(actionId);
		if (action == null) {
			return;
		}
		log(action, operatorId, operatorName, operatorLoginName, operatorType, operateObject, operateObjectName,
				operateStat, descript, ipAddress);
	}

	/**
	 * 记录日志
	 * 
	 * @param action
	 * @param operatorId
	 * @param operatorName
	 * @param operatorLoginName
	 * @param operatorType
	 * @param operateObject
	 * @param operateObjectName
	 * @param operateStat
	 * @param descript
	 * @param ipAddress
	 */
	public void log(Action action, String operatorId, String operatorName, String operatorLoginName, int operatorType,
			String operateObject, String operateObjectName, int operateStat, String descript, String ipAddress) {
		if (action == null) {
			return;
		}

		Log log = new Log();
		log.setAction(action);
		log.setTime(MiscUtil.getCurrentDate());
		log.setOperatorId(operatorId);
		log.setOperatorName(operatorName);
		log.setOperatorLoginName(operatorLoginName);
		log.setOperatorType(operatorType);
		log.setOperateObject(operateObject);
		log.setOperateObjectName(operateObjectName);
		log.setOperateStat(operateStat);
		log.setDescript(descript);
		log.setIpAddress(ipAddress);

		logDAO.save(log);
	}

}
